#ifndef CENTER_DETECTOR_HPP
#define CENTER_DETECTOR_HPP

#include <rclcpp/rclcpp.hpp>
#include "rclcpp_lifecycle/lifecycle_node.hpp"          // 必需头文件
#include "rclcpp_lifecycle/lifecycle_publisher.hpp"  // 如果有发布者
#include "lifecycle_msgs/msg/transition.hpp"        // 状态转换
#include "lifecycle_msgs/msg/state.hpp"   
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <geometry_msgs/msg/point_stamped.hpp>
#include <std_msgs/msg/header.hpp> 
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <memory>
#include <std_msgs/msg/string.hpp>
#include "center_detection/detection_manager.hpp"
#include "center_detection/neutron_detector.hpp"
#include "center_detection/gamma_detector.hpp"
#include "center_detection/center_json.hpp"



namespace center_detection {

    using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

    class CenterDetector : public rclcpp_lifecycle::LifecycleNode{

    public:
        CenterDetector();
        virtual ~CenterDetector() = default;

    private:

        // lifecycle回调函数
        CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
        CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
        CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
        CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
        //CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
        //CallbackReturn on_error(const rclcpp_lifecycle::State & state) override;



        // 原有函数
        void declare_parameters();
        void init_components();
        void setup_ros_interface();
        void cloud_callback(const sensor_msgs::msg::PointCloud2::SharedPtr msg);
        void detection_mode_callback(const std_msgs::msg::String::SharedPtr msg);
        void perform_detection(const pcl::PointCloud<pcl::PointXYZI>::Ptr& cloud);
    

        // 指数滤波函数
        geometry_msgs::msg::PointStamped applyExponentialFilter(
            const geometry_msgs::msg::PointStamped& raw_point,
            geometry_msgs::msg::Point& last_filtered);

        //ROS接口
        rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr cloud_sub_;
        rclcpp::Subscription<std_msgs::msg::String>::SharedPtr detection_mode_sub_;

        // 发布者改为LifecyclePublisher
        std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::PointStamped>> pub_neutron_center_;
        std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::PointStamped>> pub_gamma_center_;
        std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<std_msgs::msg::String>> pub_center_json_;


        //处理接口
        std::unique_ptr<DetectionManager> detection_manager_;

        // 新增json和string发布成员
        CenterJsonConverter json_coord_;
        std_msgs::msg::String json_msg_;

        // 互斥锁保护共享变量
        std::mutex detection_mode_mutex_;

        // 检测器模式选择
        std::string detection_mode_;

        // 订阅发布话题名
        std::string subscribing_cloud_;
        std::string publishing_topic_;

        geometry_msgs::msg::Point last_neutron_point_;
        geometry_msgs::msg::Point last_gamma_point_;
        double alpha_ = 0.3;  // 滤波系数，0-1之间，越小越平滑

        // 添加状态标志
        bool state_is_active_;
    };


}

#endif